//
//  SelfInspectionViewController.m
//  安好
//
//  Created by lianchuang on 14-11-10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SelfInspectionViewController.h"
#import "SelfInspectionCell.h"

#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define MoreCellLineColor [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1]

@interface SelfInspectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation SelfInspectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.symptomBaseClass) {
        self.symptomBaseClass = [[SymptomBaseClass alloc]init];
    }
    
    [self makeNavBar];
    [self makeTableView];
    
    
//    if ([responseObject isKindOfClass:[NSDictionary class]]) {
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",SYMPTOM_key]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:path]) {//没文件的情况
        NSDictionary *responseObject = [[NSDictionary alloc]initWithContentsOfFile:path];

        self.symptomBaseClass =[SymptomBaseClass modelObjectWithDictionary:responseObject];
        if (self.symptomBaseClass.data.count>0) {
            SymptomData *da = [[SymptomData alloc]init];
            da.category =@"拍照自查";
            da.cid =@"99999";
            da.count =@"百度识图";
            da.introduction =@"将患处拍下或上传手机上的照片";
            da.pic =@"99999";
            da.picurl =@"phoneselfinspection";
            [self.symptomBaseClass.data addObject:da];
            [self.tableView reloadData];
            [self loadData];

        }else{
            [self loadData];
        }

    }else{
        [self loadData];

    }
    

   
}
#pragma mark - 导航条的布局
-(void)makeNavBar
{
    self.navigationItem.title = @"自诊自查";
    //贴 返回 按钮的图片
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;

}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 加载数据
-(void)loadData
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,SYMPTOM_] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        MyNSLog("%@",responseObject);
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:[responseObject objectForKey:@"msg"]];
        }else{
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                self.symptomBaseClass = [SymptomBaseClass modelObjectWithDictionary:responseObject];


                
                
                SymptomData *da = [[SymptomData alloc]init];
                da.category =@"拍照自查";
                da.cid =@"99999";
                da.count =@"百度识图";
                da.introduction =@"将患处拍下或上传手机上的照片";
                da.pic =@"99999";
                da.picurl =@"phoneselfinspection";

                
                [self.symptomBaseClass.data addObject:da];
                
                
                NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
                // 2.2.获得文件的全路径
                NSString *path = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",SYMPTOM_key]];
                NSFileManager *fileManager = [NSFileManager defaultManager];
                
                if (![fileManager fileExistsAtPath:path]) {//没文件的情况
                    [fileManager createFileAtPath:path contents:nil attributes:nil];
                    
                    [responseObject writeToFile:path atomically:YES];
                    
                }

                
                
                [self.tableView reloadData];
            }else{
                alertContent(@"网络数据格式错误");
                [self showCustomHudSingleLine:@"网络数据格式错误"];
            }

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];


}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor =[UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        if (IPhone6Plus) {
            [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 88, 0, 0)];
        }else{
            [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 70, 0, 0)];
        }
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        if (IPhone6Plus) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 88, 0, 0)];
        }else{
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 70, 0, 0)];
        }
    }
    [self.tableView setSeparatorColor:MoreCellLineColor];

}
#pragma mark - tableView的代理方法
/**
 *  设置 各行cell高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kScreenWidth==414?64*1.2:64);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.symptomBaseClass.data.count;
}
#pragma mark --cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=nil;
    SelfInspectionCell *cell = nil;
    if (kScreenWidth == 414) {
        ID=@"tableViewCell414";
    }else{
        ID=@"tableViewCell";
    }
    cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SelfInspectionCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SelfInspectionCell" owner:self options:nil] firstObject];
        }
    }
    /**
     *  添加标题数据
     */
    cell.titleLable.text = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category];
//    /**
//     *  添加图片
//     */
//    if (indexPath.row ==self.symptomBaseClass.data.count-1) {
//        [cell.itemImgView setImage:[UIImage imageNamed:[(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] picurl]]];
//
//    }else{
//        [cell.itemImgView setImageURLStr:[(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] picurl] placeholder:nil];
//    }
    cell.itemImgView.clipsToBounds = YES;
    cell.itemImgView.layer.cornerRadius = 3;
    
    if (indexPath.row == 0) {
        
        cell.itemImgView.image = [UIImage imageNamed:@"科室自查"];
        
    }else if (indexPath.row == 1){
        
        cell.itemImgView.image = [UIImage imageNamed:@"症状自查"];
        
    }else if (indexPath.row == 2){
        cell.itemImgView.image = [UIImage imageNamed:@"急症自查"];
        
    }else if (indexPath.row == 3){
        
        cell.itemImgView.image = [UIImage imageNamed:@"常见"];
        
    }else if (indexPath.row == 4){
        
        cell.itemImgView.image = [UIImage imageNamed:@"拍照"];
    }
    
    
    /**
     *  添加描述数据
     */
//    cell.infoLable.text = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] introduction];
    
    NSString *str = [NSString stringWithFormat:@"%@",[(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category]];
    
    if ([str hasSuffix:@"自查"]) {
       str= [str substringToIndex:str.length-2];
        
    }
    
    cell.infoLable.text = [NSString stringWithFormat:@"按%@查找相关信息", str];

    /**
     *  添加count数据，网络请求--刷新
     */
    cell.totalLable.text = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] count];
//    /**
//     *  线条的处理
//     */
//    cell.bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
//    if (indexPath.row != self.symptomBaseClass.data.count - 1)
//    {
//        cell.bottomLineView.frame = CGRectMake(70, cell.frame.size.height-1,kScreenWidth-70 - 5, 0.5);
//    }
//    else
//    {
//        cell.bottomLineView.frame = CGRectMake(0, cell.frame.size.height-1, kScreenWidth, 0.5);
//        cell.infoLable.frame = CGRectMake(70,37 , 190, 17);
//    }
    
    return cell;
}
#pragma mark --选中跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row ==self.symptomBaseClass.data.count-1){
        SimilarPicViewController *similarPicViewCtroller = [[SimilarPicViewController alloc] init];
    [self.navigationController pushViewController:similarPicViewCtroller animated:YES];
    }

    
    switch (indexPath.row) {
        case 0:
        {
            WikiDepartmentViewController *wdVC = [[WikiDepartmentViewController alloc]initWithNibName:@"WikiDepartmentViewController" bundle:nil];
            [self.navigationController pushViewController:wdVC animated:YES];
        }
            break;
        case 1:
        {
            
            
            ZhengZhuangViewController *zhengZhuangViewController = [[ZhengZhuangViewController alloc] initWithNibName:@"ZhengZhuangViewController" bundle:nil];
            zhengZhuangViewController.title = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category];

            [self.navigationController pushViewController:zhengZhuangViewController animated:YES];
        }
            break;
        case 2:
        {
            //急诊自查
            
            JiZhenViewController *jiZhenViewController = [[JiZhenViewController alloc] init];
            jiZhenViewController.title = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category];
            jiZhenViewController.appendingString = @"/symptom/eme";
            jiZhenViewController.title = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category];
            [self.navigationController pushViewController:jiZhenViewController animated:YES];
        }
            break;
        case 3:
        {
            ChangJianBingViewController *cjbVC = [[ChangJianBingViewController alloc]initWithNibName:@"ChangJianBingViewController" bundle:nil];
            cjbVC.title = [(SymptomData *)[self.symptomBaseClass.data objectAtIndex:indexPath.row] category];
            [self.navigationController pushViewController:cjbVC animated:YES];
        }
            break;
            
            
            
        
            
        default:
            break;
    }

}



@end
